package com.atlin.project.controller.task;

import com.atlin.project.annotation.AuthCheck;
import com.atlin.project.common.BaseResponse;
import com.atlin.project.common.DeleteRequest;
import com.atlin.project.common.ErrorCode;
import com.atlin.project.common.ResultUtils;
import com.atlin.project.contant.UserConstant;
import com.atlin.project.exception.BusinessException;
import com.atlin.project.exception.ThrowUtils;
import com.atlin.project.model.domain.SysUser;
import com.atlin.project.model.domain.task.TaskTags;
import com.atlin.project.model.dto.taskTags.TaskTagsAddRequest;
import com.atlin.project.model.dto.taskTags.TaskTagsQueryRequest;
import com.atlin.project.model.dto.taskTags.TaskTagsUpdateRequest;
import com.atlin.project.model.vo.TaskTagsVO;
import com.atlin.project.service.SysUserService;
import com.atlin.project.service.task.TaskTagsService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.gson.Gson;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 模版接口
 *
 * @author atlin
 */
@RestController
@RequestMapping("/taskTags")
public class TaskTagsController {

    @Resource
    private TaskTagsService taskTagsService;

    @Resource
    private SysUserService sysUserService;

    private final static Gson GSON = new Gson();

    // region 增删改查

    /**
     * 创建
     *
     * @param taskTagsAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addTaskTags(@RequestBody TaskTagsAddRequest taskTagsAddRequest, HttpServletRequest request) {
        if (taskTagsAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TaskTags taskTags = new TaskTags();
        BeanUtils.copyProperties(taskTagsAddRequest, taskTags);

        taskTagsService.valid(taskTags, true);
        SysUser loginUser = sysUserService.getLoginUser(request);

        // todo

        boolean result = taskTagsService.save(taskTags);
        ThrowUtils.throwIf(!result, ErrorCode.OPERATION_ERROR);
        long newTaskTagsId = taskTags.getId();
        return ResultUtils.success(newTaskTagsId);
    }

    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteTaskTags(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        SysUser user = sysUserService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        TaskTags oldtaskTags = taskTagsService.getById(id);
        ThrowUtils.throwIf(oldtaskTags == null, ErrorCode.NULL_ERROR);

        // 仅本人或管理员可删除

        boolean b = taskTagsService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新（仅管理员）
     *
     * @param taskTagsUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateTaskTags(@RequestBody TaskTagsUpdateRequest taskTagsUpdateRequest) {
        if (taskTagsUpdateRequest == null || taskTagsUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TaskTags taskTags = new TaskTags();
        BeanUtils.copyProperties(taskTagsUpdateRequest, taskTags);

        // 参数校验
        taskTagsService.valid(taskTags, false);
        long id = taskTagsUpdateRequest.getId();
        // 判断是否存在
        TaskTags oldtaskTags = taskTagsService.getById(id);
        ThrowUtils.throwIf(oldtaskTags == null, ErrorCode.NULL_ERROR);
        // todo other operation

        boolean result = taskTagsService.updateById(taskTags);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<TaskTags> getTaskTagsById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TaskTags taskTags = taskTagsService.getById(id);
        if (taskTags == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        SysUser loginUser = sysUserService.getLoginUser(request);
        // 不是本人或管理员，不能直接获取所有信息

        return ResultUtils.success(taskTags);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<TaskTagsVO> getTaskTagsVOById(long id, HttpServletRequest request) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        TaskTags taskTags = taskTagsService.getById(id);
        if (taskTags == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }

        TaskTagsVO taskTagsVO = TaskTagsVO.objToVo(taskTags);
        return ResultUtils.success(taskTagsVO);
    }

    /**
     * 分页获取列表（封装类）
     *
     * @param taskTagsQueryRequest 查询参数
     * @param request              查询请求
     * @return 分页封装类
     */
    @PostMapping("/list/page/vo")
    public BaseResponse<Page<TaskTagsVO>> listTaskTagsVOByPage(@RequestBody TaskTagsQueryRequest taskTagsQueryRequest,
                                                               HttpServletRequest request) {
        long current = taskTagsQueryRequest.getCurrent();
        long size = taskTagsQueryRequest.getPageSize();
        // 限制爬虫
        ThrowUtils.throwIf(size > 20, ErrorCode.PARAMS_ERROR);
        Page<TaskTags> taskTagsPage = taskTagsService.page(new Page<>(current, size),
                taskTagsService.getQueryWrapper(taskTagsQueryRequest));

        Page<TaskTagsVO> taskTagsVOPage = taskTagsService.getTaskTagsVOPage(taskTagsPage, request);

        return ResultUtils.success(taskTagsVOPage);
    }

    /**
     * 分页获取列表（仅管理员）
     *
     * @param taskTagsQueryRequest 查询参数
     * @param request              查询请求
     * @return 分页
     */
    @PostMapping("/list/page")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<TaskTags>> listTaskTagsByPage(@RequestBody TaskTagsQueryRequest taskTagsQueryRequest,
                                                           HttpServletRequest request) {
        long current = taskTagsQueryRequest.getCurrent();
        long size = taskTagsQueryRequest.getPageSize();
        Page<TaskTags> taskTagsPage = taskTagsService.page(new Page<>(current, size),
                taskTagsService.getQueryWrapper(taskTagsQueryRequest));
        return ResultUtils.success(taskTagsPage);
    }

    // endregion

}
